<?php

namespace app\common\model\vod;

use tencentcloud\Vod;

class VideoModel
{
  private $vodModel;

  //初始化
  public function __construct()
  {
    $this->vodModel = new Vod();
  }

  /**
   * 获取单个视频信息
   * @param $FileId
   * @return mixed
   */
  public function getVideoInfo($FileId){
    $infos = $this->getVideoInfos($FileId);
    return $infos[0];
  }

  /**
   * 获取多个视频详情
   * @param $videoIds
   * @param array $Filters
   * @return mixed
   */
  public function getVideoInfos($videoIds, $Filters = ["basicInfo", "metaData"])
  {
    $vids = is_array($videoIds) ? $videoIds : explode(",", $videoIds);
    $list      = $this->vodModel->execFun('DescribeMediaInfos', [
        "FileIds" => $vids,
        "Filters" => $Filters
    ]);
    $videoList = [];
    foreach ($list['MediaInfoSet'] as $k => $v) {
      $info = $v['BasicInfo'];
      $info['FileId'] = $v['FileId'];
      if(empty($info['CoverUrl']) || mb_strpos($info['CoverUrl'],"default-cover.png") !== false){
        $info['CoverUrl'] = '/static/system/images/default-cover.png';
      }
      $info['TagSet'] = empty($info['TagSet']) ? '' : implode(',',$info['TagSet']);
      $videoList[] = array_merge($info, $v['MetaData']);
    }
    return $videoList;
  }

  /**
   * 获取分页列表
   * @param $pageSize
   * @param $pageNumber
   * @param $q
   * @return array
   */
  public function searchMedia($pageNumber, $pageSize, $q)
  {
    $query = [
        'Offset' => ($pageNumber - 1) * $pageSize,
        'Limit' => $pageSize
    ];
    $query = array_merge($query, $q);
    $data  = ['rows' => [], 'total' => 0];
    try {
      $list = $this->vodModel->execFun('SearchMedia', $query);
      if ($list['TotalCount'] > 0) {
        $rows = [];
        foreach ($list['MediaInfoSet'] as $v) {
          $item = $v['BasicInfo'];
          $item['FileId'] = $v['FileId'];
          if(empty($item['CoverUrl']) || mb_strpos($item['CoverUrl'],"default-cover.png") !== false){
            $item['CoverUrl'] = '/static/system/images/default-cover.png';
          }
          $rows[] = $item;
        }
        $data = ['rows' => $rows, 'total' => $list['TotalCount']];
      }
    } catch (\Throwable $th) {
      $data['rows']    = [];
      $data['total']   = 0;
      $data['message'] = $th->getMessage();
    }
    return $data;
  }

  /**
   * 修改视频信息
   * @param $data //VideoId Title Tags Description CoverURL CateId CustomMediaInfo
   * @return bool
   * @throws \Exception
   */
  public function edit($data)
  {
    foreach ($data as $k => $v) {
      if (empty($data[$k])) unset($data[$k]);
    }
    if(isset($data['ClassId'])) $data['ClassId'] = (int)$data['ClassId'];
    if(!empty($data['AddTags'])) $data['AddTags'] = explode(',',$data['AddTags']);
    if(!empty($data['TagSet'])){
      $data['TagSet'] = explode(',',$data['TagSet']);
      $addTags = empty($data['AddTags']) ? [] : $data['AddTags'];
      $diffTag = array_diff($data['TagSet'],$addTags); //要删除的tag
      if(!empty($diffTag)) $data['DeleteTags'] = array_values($diffTag);
    }
    if(!empty($data['CoverData'])){
      //去除base64头部
      $data['CoverData'] = substr(strstr($data['CoverData'],','),1);
    }
    $this->vodModel->execFun('ModifyMediaInfo', $data);
    return true;
  }

  /**
   * 删除视频
   * @param $id
   * @return bool
   * @throws \Exception
   */
  public function del($id)
  {
    $this->vodModel->execFun('DeleteMedia', [
        'FileId' => $id
    ]);
    return true;
  }

}